import {
	amapKey
} from '@/config/index'
import {
	AMapWXs
} from '@/util/amap-wx.130.js'


export function getLocation(isForce = false, showToast = true) {
	return new Promise((resolve) => {
		uni.getLocation({
			isHighAccuracy: true,
			type: 'gcj02',
			success: (res) => {
				resolve(res)
			},
			fail: () => {
				if (showToast === true) {
					uni.showToast({
						title: '暂未获取定位信息',
						icon: 'none',
					})
				}
				if (isForce) {
					uni.getSetting({
						success(res) {
							if (
								res.authSetting['scope.userLocation'] === undefined ||
								res.authSetting['scope.userLocation'] === false
							) {
								uni.showModal({
									title: '提示',
									content: '您拒绝了定位权限，是否前往授权？',
									success: (res2) => {
										if (res2.confirm) {
											uni.openSetting({
												success: (res) => {
													if (res
														.authSetting[
															'scope.userLocation'
														]) {
														uni.getLocation({
															type: 'gcj02',
															success: (
																res
															) => {
																resolve
																	(
																		res
																		)
															},
															fail: (
																err
																) => {
																console
																	.error(
																		'获取位置信息失败',
																		err
																	)
															},
														})
													} else {
														// 没有允许定位权限
														uni.showToast({
															title: '没有允许定位权限',
															icon: 'none',
														})
													}
												},
											})
										}
									},
									fail: () => {
										resolve({})
									},
								})
							} else {
								resolve({
									getSetting: false
								})
							}
						},
						fail() {
							resolve({
								getSetting: false
							})
						},
					})
				} else {
					resolve({})
				}
			},
		})
	})
}

export function getAmapLocation(isForce = false, showToast = true) {
	return new Promise((resolve) => {
		// 获取当前位置
		const myAmapFun = new AMapWXs({
			key: amapKey
		})
		myAmapFun.getRegeo({
			success: (res) => {
				resolve(res[0])
			},
			fail: () => {
				if (showToast === true) {
					uni.showToast({
						title: '暂未获取定位信息',
						icon: 'none',
					})
				}
				if (isForce) {
					uni.getSetting({
						success(res) {
							if (
								res.authSetting['scope.userLocation'] === undefined ||
								res.authSetting['scope.userLocation'] === false
							) {
								uni.showModal({
									title: '提示',
									content: '您拒绝了定位权限，是否前往授权？',
									success: (res2) => {
										if (res2.confirm) {
											uni.openSetting({
												success: (res) => {
													if (res
														.authSetting[
															'scope.userLocation'
														]) {
														myAmapFun
															.getRegeo({
																success: (
																	res
																) => {
																	resolve
																		(res[
																			0
																			])
																},
															})
													} else {
														// 没有允许定位权限
														uni.showToast({
															title: '没有允许定位权限',
															icon: 'none',
														})
													}
												},
											})
										}
									},
								})
							} else {
								resolve({
									getSetting: false
								})
							}
						},
						fail() {
							resolve({
								getSetting: false
							})
						},
					})
				} else {
					resolve({})
				}
			},
		})
	})
}

export const unis = {
	hideLoading() {
		uni.hideLoading({
			success: () => {}
		})
	},
	hideToast() {
		uni.hideToast({
			success: () => {}
		})
	},
}